home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip0493.zip / CRYPT.C < prev    next >
Text File  |  1993-04-05  |  3KB  |  48 lines

  1. /****************************************************************/
  2. /*                                                              */
  3. /*      S-CODER - Encrypt/decrypt data                          */
  4. /*                                                              */
  5. /*      Copyright 1987-1989 by Robert B. Stout dba MicroFirm    */
  6. /*                                                              */
  7. /*      Originally written by Bob Stout with modifications      */
  8. /*      suggested by Mike Smedley.                              */
  9. /*                                                              */
  10. /*      This code may be used freely in any program for any     */
  11. /*      application, personal or commercial.                    */
  12. /*                                                              */
  13. /*  Current commercial availability:                            */
  14. /*                                                              */
  15. /*      1. MicroFirm Toolkit ver 3.00: LYNX and CRYPT utilities */
  16. /*      2. CXL libraries (MSC, TC, ZTC/C++, PC): fcrypt()       */
  17. /*         dedicated file encryption function                   */
  18. /*      3. SMTC & MFLZT libraries: crypt() function             */
  19. /*                                                              */
  20. /****************************************************************/
  21.  
  22. char *cryptext;         /* The actual encryption/decryption key */
  23. int   crypt_ptr = 0;    /* Circular pointer to elements of key  */
  24. int   crypt_length;     /* Set externally to strlen(cryptext)   */
  25.  
  26. /* NOTES: cryptext should be set and qualified (to something over
  27.           5-6 chars, minimum) by the calling program, which should
  28.           also set crypt_ptr in the range of 0 to strlen(cryptext)
  29.           before each use. If crypt() is used to encrypt several
  30.           buffers, cryptext should be reloaded and crypt_ptr reset
  31.           before each buffer is encrypted. The encryption is both
  32.           reversible - to decrypt data, pass it back through crypt()
  33.           using the original key and original initial value of
  34.           crypt_ptr - and multiple passes are commutative.
  35. */
  36.  
  37. /**** Encrypt/decrypt buffer datum ******************************/
  38. void crypt(char *buf)
  39. {
  40.         *buf ^= cryptext[crypt_ptr] ^ (cryptext[0] * crypt_ptr);
  41.         cryptext[crypt_ptr] += ((crypt_ptr < (crypt_length - 1)) ?
  42.                 cryptext[crypt_ptr + 1] : cryptext[0]);
  43.         if (!cryptext[crypt_ptr])
  44.                 cryptext[crypt_ptr] += 1;
  45.         if (++crypt_ptr >= crypt_length)
  46.                 crypt_ptr = 0;
  47. }
  48.